﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DirectorySearching
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder builder=new StringBuilder();
            Traverse(@"C:\Users\Academy\Documents\Visual Studio 2010\Projects","Milen",builder);
            StreamWriter writer = new StreamWriter("occurences.txt");
            using (writer)
            {
                writer.Write(builder);
            }
        }

        private static void Traverse(string path,string str,StringBuilder fileText)
        {
            string[] subdirs = Directory.GetDirectories(path);
            foreach (string subdir in subdirs)
            {
                Traverse(subdir, str, fileText);
            }
            string[] files = Directory.GetFiles(path,"*.txt");
            foreach (string f in files)
            {
                StreamReader reader = new StreamReader(f);
                using (reader)
                {
                    string line = reader.ReadLine();
                    int lineNum = 1;
                    while (line != null)
                    {
                        int p = line.IndexOf(str);
                        if (p > 0)
                        {
                           fileText.Append(f+" "+lineNum+"\n");
                           break;
                        }
                        lineNum++;
                        line = reader.ReadLine();
                    }
                }
            }
        }

    }
}
